/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import Beans.UserBean;
import Processes.Navigate;
import java.io.Serializable;
import javax.ejb.EJB;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import radio.entities.AccountDetails;
import radio.entities.Accounts;
import radio.entities.City;
import radio.sessions.AccountsFacade;
import radio.sessions.CityFacade;

/**
 *
 * @author Kim
 */
public class User implements Serializable {

    @EJB
    private CityFacade cityFacade;
    @EJB
    private AccountsFacade accountsFacade;
    private String Newpwd, Confirmnew, Oldpwd, cityName;

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getConfirmnew() {
        return Confirmnew;
    }

    public void setConfirmnew(String Confirmnew) {
        this.Confirmnew = Confirmnew;
    }

    public String getNewpwd() {
        return Newpwd;
    }

    public void setNewpwd(String Newpwd) {
        this.Newpwd = Newpwd;
    }

    public String getOldpwd() {
        return Oldpwd;
    }

    public void setOldpwd(String Oldpwd) {
        this.Oldpwd = Oldpwd;
    }
    private UserBean user;

    public UserBean getUser() {
        return user;
    }

    public void setUser(UserBean user) {
        this.user = user;
    }

    /** Creates a new instance of User */
    public User() {
    }

    public User(UserBean user, AccountsFacade accountsFacade, CityFacade cityFacade) {
        this.user = user;
        this.accountsFacade = accountsFacade;
        this.cityFacade = cityFacade;
    }

    public String edit() {

        if (!this.user.getPwd().equals(this.getOldpwd())) {
            FacesContext cxt = FacesContext.getCurrentInstance();
            cxt.addMessage("EditForm:old", new FacesMessage("Old Password does not match"));
            this.setNewpwd(null);
            this.setConfirmnew(null);
            this.setOldpwd(null);
            return "";
        } else if (!this.getConfirmnew().equals(this.getNewpwd())) {
            FacesContext cxt = FacesContext.getCurrentInstance();
            cxt.addMessage("EditForm:confirm", new FacesMessage("Confirm Password does not match"));
            this.setOldpwd(null);
            this.setNewpwd(null);
            this.setConfirmnew(null);
            return "";
        } else {
            Accounts acc = accountsFacade.findbyId(this.user.getAccId());
            AccountDetails detail = acc.getAccountDetails();
//            java.util.Date dd=new java.util.Date();
//            Date d=new Date(new java.util.Date().getTime());                        
            detail.setAddress(this.user.getAddress());
            detail.setContactPerson(this.user.getContact());
            detail.setEmail(this.user.getEmail());
            detail.setMobile(this.user.getMobile());
            detail.setTelephone(this.user.getTele());
            City city = cityFacade.find(this.user.getCityId());
            detail.setCityId(city);
            this.cityName = city.getCityName();
            acc.setPwd(this.getNewpwd());
            acc.setGender(this.user.isGender());
            accountsFacade.edit(acc);
            this.user.setPwd(this.getNewpwd());
            this.setOldpwd(null);
            this.setNewpwd(null);
            this.setConfirmnew(null);

            Navigate nav = (Navigate) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("navigate");
            nav.setUserpage("./ViewAccount.xhtml");
            return "UserPage.xhtml";
        }
    }
}